var app = getApp()

Page({
  data: {
    orders: {},
    url: {
      selectOne: "../orderinfo/orderinfo",
      takeOrder: app.data.ull + "wxTakeOrder",
    },
    allorder: "",
    index: 0,
    Showview: "none",
    fettlerid: "",
    location: {},
    markers: [],
    callout: {
      content: "",
      bgColor: "yellow",
      display: 'BYCLICK',
    },
    orderunTake1:{},
    orderunTake2: {},
    orderunTake3: {},
    orderunTake4: {},
  },
  onLoad: function () {
    var that = this;
    var markers = that.data.markers;
    
    wx.getLocation({
      success: function (res) {
        var marker = {
          id: 0,
          latitude: res.latitude,
          longitude: res.longitude,
          callout: {
            content: "当前",
            color: "black",
            bgColor: "green",
            display: 'ALWAYS',
          },

        };
        markers.push(marker);
        that.setData({
          markers: markers,
          location: res,
        })
      }
    })
  },
  takeOrder: function (e) {
    var that = this;
    var orderid = that.data.allorder[e.target.id].number;
    wx.showModal({
      title: '是否接单',
      content: '',
      confirmText: '确认接单',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.url.takeOrder,
            data: {
              orderId: orderid,
              fettlerId: app.data.user.id,
            },
            header: {},
            method: "GET",
            dataType: "json",
            success: function (res) {
              wx.navigateTo({
                url: that.data.url.selectOne + '?orderId=' + orderid,
              })
            },
            fail: function (res) { },
            complete: function (res) {
            },
          })
        }
      }

    })
  },
  onShow: function (options) {
    wx.showNavigationBarLoading();
    var that = this;
    //未接单
    wx: wx.request({
      url: that.data.url.selectAll,
      data: {
        userid:app.data.user.id,
        status:0
      },
      header: {},
      method: "GET",
      dataType: "json",
      success: function (res) {
        var showview = "";
        var allorder = res.data;
        var markers = that.data.markers;
        if (res.data.length == 0) {
          showview = "block";
        } else {
          showview = "none";
        }
        that.setData({
          orderunTake1: allorder,
          showview: showview,
        })
        console.log("order", that.data.orderunTake1);
      
      },
      fail: function (res) { },
      complete: function (res) { },
    });
    //已接单
    wx: wx.request({
      url: that.data.url.selectAll,
      data: {
        userid: app.data.user.id,
        status: 1
      },
      header: {},
      method: "GET",
      dataType: "json",
      success: function (res) {
        var showview = "";
        var allorder = res.data;
        var markers = that.data.markers;
        if (res.data.length == 0) {
          showview = "block";
        } else {
          showview = "none";
        }
        that.setData({
          orderunTake2: allorder,
          showview: showview,
        })
        console.log("order", that.data.orderunTake2);
        
      },
      fail: function (res) { },
      complete: function (res) { },
    });
  },

  payorder: function (e) {
    var orderid = e.target.dataset.id;
    wx.setStorageSync('orderid', orderid)
    uctooPay.generateOrder();
  },
  switchSlider: function (e) {
    this.setData({
      current: e.target.dataset.index
    })
  },
  changeSlider: function (e) {
    this.setData({
      current: e.detail.current
    })
  },
  delorder: function (e) {

  },
  getOrders: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.type;
    console.log(status);
    wx.request({
      url: app.data.ull+'getOrdersByStatus',
      data: { userid: app.data.user.id, status: status },
      success: function (data) {
        console.log(data);
        that.setData({
          orders: data.data
        })
        console.log(that.data.orders);
      }
    })
  }
})